import {Component,OnInit,ViewChild} from '@angular/core';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';

@Component({
  selector: 'product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.scss'],
  animations: [
      flyIn
  ]
})
export class Product implements OnInit{  
  @ViewChild('myModal') public myModal:ModalDirective;
  proData:any;
  proname:any;
  prodes:any;
  product_name:any;
  product_description:any;
  productname:any;
  productdescription:any;
  product_id:any;
  proconnect:any;
  
  btype:number;
  constructor(private myHttp:MyHttp) {}
  ngOnInit() {   
    this.getdata();
  }

  cancel(){
    this.productname="";
    this.productdescription="";    
  }

  getdata(){
    this.myHttp.post('product/findAll',{}).subscribe(res=>{       
      if(res.errorCode==0){
        this.proData = res.message;
         //console.log(this.proData)
      }   
      
    })
  }

  addsave(){
    this.myHttp.post('product/create',{
      "product_name" : this.proname,
      "product_description" : this.prodes,
      "product_connection":this.proconnect
    }).subscribe(res=>{
      if(res.errorCode==0){
         alert(res.message);
         this.myModal.hide();
         this.getdata();
         this.prodes = '';
         this.proname = '';
         //console.log(this.proData)
      }   
      
    })
  }

  editproduct(pdata){
    this.myModal.show();
    this.btype = 2;
    this.product_id = pdata.id;
    this.productname = pdata.product_name;
    this.productdescription = pdata.product_description;
  }

  editsave(){
    //console.log('sdad')
    this.myHttp.post('product/update',{
      "product_id" :this.product_id,
      "product_name" : this.productname,
      "product_description" : this.productdescription
    }).subscribe(res=>{      
      if(res.errorCode==0){
         alert(res.message);
         this.myModal.hide();
         this.getdata();       
         //console.log(this.proData)
      }   
      
    })
  }

  delproduct(prodata){
      this.btype = 3;
      this.myModal.show();
      this.product_id = prodata.id;
      this.product_name = prodata.product_name;
  }
  delsave(){
    this.myHttp.post('product/delete',{
      "product_id" :this.product_id     
    }).subscribe(res=>{      
      if(res.errorCode==0){
         alert(res.message);
         this.myModal.hide();
         this.getdata();       
         
      }   
      
    })
  }


}

